package cn.gs.serializable.serialize;

// @author gs
import cn.gs.serializable.entity.IEntity;
import cn.gs.serializable.entity.OneEntity;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class ConstSerialize implements ISerialize {

    private Map<String, Object> consts = new HashMap<String, Object>();
    private Map<Object, String> consto = new HashMap<Object, String>();

    public ConstSerialize(Map<Object, String> con) {
        for (Entry<Object, String> e : con.entrySet()) {
            consto.put(e.getKey(), e.getValue());
            consts.put(e.getValue(), e.getKey());
        }
    }

    @Override
    public <T> T toObj(cn.gs.serializable.serialize.ISerialize ctrl, IEntity from, Class<T> clazz) {
        if (from != null && from instanceof OneEntity) {
            String t = ((OneEntity) from).getValue();
            return (T) consts.get(t.toLowerCase());
        }
        return null;
    }

    @Override
    public IEntity fromObj(ISerialize ctrl, Object from) {
        String t = consto.get(from);
        if (t != null) {
            return new OneEntity(t);
        }
        return null;
    }
}
